
#include "main.h"

#define  CH1_PORT   GPIOB
#define  CH1_PIN    GPIO_Pin_13

#define  CH2_PORT   GPIOB
#define  CH2_PIN    GPIO_Pin_14

#define  CH3_PORT   GPIOB
#define  CH3_PIN    GPIO_Pin_15

#define  CH1N_PORT  GPIOA
#define  CH1N_PIN   GPIO_Pin_8

#define  CH2N_PORT  GPIOA
#define  CH2N_PIN   GPIO_Pin_9

#define  CH3N_PORT  GPIOA
#define  CH3N_PIN   GPIO_Pin_10


//=========================================================
//=========================================================
void setup_pwm(unsigned char channel,unsigned char on,unsigned char duty)
{
	switch(channel)
	{
	  case 0:
		  if(on==0)
        TIM_SetCompare1(TIM1,0);
			else
			{
				if(duty==0)
					TIM_SetCompare1(TIM1,1);
				else
				  TIM_SetCompare1(TIM1,duty*72);
		  }
			break;
		case 1:
		  if(on==0)
        TIM_SetCompare2(TIM1,0);
			else
			{
				if(duty==0)
					TIM_SetCompare2(TIM1,1);
				else
				  TIM_SetCompare2(TIM1,duty*72);
		  }
			break;
		case 2:
		  if(on==0)
        TIM_SetCompare3(TIM1,0);
			else
			{
				if(duty==0)
					TIM_SetCompare3(TIM1,1);
				else
				  TIM_SetCompare3(TIM1,duty*72);
		  }
			break;
	}
}

//=========================================================
//=========================================================
void Pwm_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_TIM1, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
	
	{
	  GPIO_InitTypeDef GPIO_InitStructure;

	  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
	  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	  GPIO_Init( GPIOA, &GPIO_InitStructure );
	
	  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
	  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	  GPIO_Init( GPIOB, &GPIO_InitStructure );
	}
	
	{
    TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
	
    TIM_BaseInitStructure.TIM_Period = 7200;
    TIM_BaseInitStructure.TIM_Prescaler = 1-1;
    TIM_BaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure);
	
    TIM_ARRPreloadConfig(TIM1, ENABLE);  
  }
	
	{
	  TIM_OCInitTypeDef TIM_OCInitStructure;
	
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;	 
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; 
    TIM_OCInitStructure.TIM_Pulse = 72*20;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
    TIM_OCInitStructure.TIM_OCNPolarity= TIM_OCNPolarity_Low;
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
  
    TIM_OC1Init(TIM1, &TIM_OCInitStructure);
    TIM_OC2Init(TIM1, &TIM_OCInitStructure);
    TIM_OC3Init(TIM1, &TIM_OCInitStructure);
		
    TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
    TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);
    TIM_OC3PreloadConfig(TIM1,TIM_OCPreload_Enable);
  }
	
	{
	  TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
    TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
    TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
    TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
    TIM_BDTRInitStructure.TIM_DeadTime = 1;
    TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
    TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
    TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
    TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
		
    TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure);  
	}
	
	TIM_CtrlPWMOutputs(TIM1,ENABLE);
	TIM_Cmd( TIM1, ENABLE );
	
	{  //                         AB     AC     BC     BA     CA     CB     
	  int PHASE_CHANGE[7]={0x0000,0x0005,0x0005,0x0050,0x0050,0x0500,0x0500};
	
	  while(1)
    {		
			TIM_SelectOCxM(TIM1,TIM_Channel_3,TIM_ForcedAction_Active);
			
			TIM_SelectOCxM(TIM1,TIM_Channel_1,TIM_OCMode_PWM2);
			TIM_SelectOCxM(TIM1,TIM_Channel_2,TIM_OCMode_PWM2);
			TIM1->CCER=PHASE_CHANGE[1];

			Delay_Ms(10);
			
			TIM_SelectOCxM(TIM1,TIM_Channel_2,TIM_ForcedAction_Active);
			TIM_SelectOCxM(TIM1,TIM_Channel_1,TIM_OCMode_PWM2);
			TIM_SelectOCxM(TIM1,TIM_Channel_3,TIM_OCMode_PWM2);
			TIM1->CCER=PHASE_CHANGE[2];
			Delay_Ms(15);
			
			TIM_SelectOCxM(TIM1,TIM_Channel_1,TIM_ForcedAction_Active);
			TIM_SelectOCxM(TIM1,TIM_Channel_2,TIM_OCMode_PWM2);
			TIM_SelectOCxM(TIM1,TIM_Channel_3,TIM_OCMode_PWM2);
			TIM1->CCER=PHASE_CHANGE[3];
			Delay_Ms(20);
			
			TIM_SelectOCxM(TIM1,TIM_Channel_3,TIM_ForcedAction_Active);
			TIM_SelectOCxM(TIM1,TIM_Channel_2,TIM_OCMode_PWM2);
			TIM_SelectOCxM(TIM1,TIM_Channel_1,TIM_OCMode_PWM2);
			TIM1->CCER=PHASE_CHANGE[4];
			Delay_Ms(25);
			
			TIM_SelectOCxM(TIM1,TIM_Channel_2,TIM_ForcedAction_Active);
			TIM_SelectOCxM(TIM1,TIM_Channel_3,TIM_OCMode_PWM2);
			TIM_SelectOCxM(TIM1,TIM_Channel_1,TIM_OCMode_PWM2);
			TIM1->CCER=PHASE_CHANGE[5];
			Delay_Ms(30);
			
			TIM_SelectOCxM(TIM1,TIM_Channel_1,TIM_ForcedAction_Active);
			TIM_SelectOCxM(TIM1,TIM_Channel_3,TIM_OCMode_PWM2);
			TIM_SelectOCxM(TIM1,TIM_Channel_2,TIM_OCMode_PWM2);
			TIM1->CCER=PHASE_CHANGE[6];
			Delay_Ms(35);
	  }
	}
}

	
  


  





